package com.example.mq.mqserver.core;

import java.io.Serializable;
import java.util.UUID;

public class Message implements Serializable {

    //核心属性
    private BasicProperties basicProperties = new BasicProperties();

    private byte[] body;


    private transient long offsetBeg = 0;//无需序列化

    private transient long offsetEnd = 0;//无需序列化

    private byte isValid = 0x1;//0x1有效，0x0无效


    //格外补充(方便后期使用)

    /**
     * 工厂方法
     * 这个方法会生成唯一的ID
     */
    public static Message createMessageWithId(String routingKey, BasicProperties basicProperties, byte[] body) {

        Message message = new Message();
        if (basicProperties != null) {
            message.setBasicProperties(basicProperties);
        }
        //不存在创建
        message.setMessageId("M-" + UUID.randomUUID());
        message.basicProperties.setRoutingKey(routingKey);
        message.body = body;
        return message;
    }


    public String getMessageId() {
        return basicProperties.getMessageId();
    }

    public void setMessageId(String messageId) {
        basicProperties.setMessageId(messageId);
    }

    public String getRoutingKey() {
        return basicProperties.getRoutingKey();
    }


    public void setRoutingKey(String routingKey) {
        basicProperties.setRoutingKey(routingKey);
    }


    public int getDeliverMode() {
        return basicProperties.getDeliverMode();
    }

    public void setDeliverMode(int deliverMode) {
        basicProperties.setDeliverMode(deliverMode);
    }


    //---------------getter+setter-------------
    public BasicProperties getBasicProperties() {
        return basicProperties;
    }

    public void setBasicProperties(BasicProperties basicProperties) {
        this.basicProperties = basicProperties;
    }

    public byte[] getBody() {
        return body;
    }

    public void setBody(byte[] body) {
        this.body = body;
    }

    public long getOffsetBeg() {
        return offsetBeg;
    }

    public void setOffsetBeg(long offsetBeg) {
        this.offsetBeg = offsetBeg;
    }

    public long getOffsetEnd() {
        return offsetEnd;
    }

    public void setOffsetEnd(long offsetEnd) {
        this.offsetEnd = offsetEnd;
    }

    public byte getIsValid() {
        return isValid;
    }

    public void setIsValid(byte isValid) {
        this.isValid = isValid;
    }
}
